/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cse308;

import cse308.entities.Users;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Query;

/**
 *
 * @author dwysocki
 */
@ManagedBean(name = "userSessionBean")
@SessionScoped
public class UserSessionBean implements Serializable {

    private List users;
    private Map<String, String> degreePrograms;
    private final static Logger LOG = Logger.getLogger(GUIUtilityClass.class.getName());
    private String username;
    private String password;
    private boolean cicmember;
    private boolean evaluator;

    public UserSessionBean() {
        LOG.info("CREATING USERSESSIONBEAN");
    }

    public boolean isEvaluator() {
        return evaluator;
    }

    public void setEvaluator(boolean evaluator) {
        this.evaluator = evaluator;
    }

    
    public boolean isCicmember() {
        return cicmember;
    }

    public void setCicmember(boolean cicmember) {
        this.cicmember = cicmember;
    }

    
    public String getUsername() {
        return username;
    }

    public void setUsername(String userName) {
        this.username = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List getUsers() {
        return users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    public String verify() {
        LOG.info("Username: " + this.username);
        LOG.info("Password: " + this.password);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CSE_308_ABETPU");
        EntityManager em = emf.createEntityManager();
        Query q = em.createNamedQuery("Users.findByUP").setParameter("username", this.username).setParameter("password", this.password);
        List<Users> l = (List<Users>)q.getResultList();
        if (l.isEmpty()) {
            LOG.info("List of matching users is empty");
            FacesMessage message = new FacesMessage();
            message.setDetail("Invalid Login");
            FacesContext.getCurrentInstance().addMessage("form", message);
            return null;
        }
        cicmember = l.get(0).getCicMember();
        evaluator = l.get(0).getEvaluator();
        q = em.createNamedQuery("Users.findAll");
        users = q.getResultList();
        LOG.info("Successful Login");
        return "WelcomePage";
    }
}
